#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
数据统计
Created by gongp on 2017/9/9.
"""

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json

from django.shortcuts import render, reverse, redirect
from django.contrib.auth import get_user
from django.contrib.auth.decorators import login_required, permission_required

from django.http import HttpResponse, HttpResponseRedirect

from smAdmin.models import WxmpScanLog, SmerpErpinfo, SmerpAccount, SmerpBrand, SmerpProduct, SmerpMaterial, CHECK_STATUS_CHOICES

from smAdmin.jqgrid import JqGrid

import logging

logger = logging.getLogger('smAdmin')


# ------------- 扫码记录 ------------------
class WxmpScanLogGrid(JqGrid):
	model = WxmpScanLog  # could also be a queryset
	fields = ['id',
			  'create_time',
			  'smcode_no',
			  'product_name',
			  'buyer_name',
			  'scan_province',
			  'scan_city',
			  'publish_province',
			  'publish_price',
			  'myac'
			  ]

	url = '/smAdmin/statistics/scan_log_list'
	editurl = '/smAdmin/statistics/scan_log_edit'
	caption = u'扫码记录'  # optional
	colmodel_overrides = {  # optional
		'id': {'key': True, 'width': 20, "hidden": True},
		'create_time': {
			"label": u"创建时间",
			"sortable": False,

			"search": True,
			"sorttype": 'date',
			"searchoptions": {"sopt": ['le', 'lt', 'gt', 'ge']},

			'editable': False,
			# "edittype": 'text',
			# "editoptions": {"size": "20", "maxlength": "40"},

			'width': 100
		},

		'smcode_no': {
			"label": u"码序号",
			"sortable": False,

			"search": False,
			"sorttype": 'string',
			"searchoptions": {"sopt": ['le', 'lt', 'gt', 'ge']},

			'editable': False,
			# "edittype": 'text',
			# "editoptions": {"size": "20", "maxlength": "40"},

			'width': 100
		},

		'product_name': {
			"label": u"产品",
			"sortable": False,

			"search": False,
			"sorttype": 'string',
			"searchoptions": {"sopt": ['le', 'lt', 'gt', 'ge']},

			'editable': False,
			# "edittype": 'text',
			# "editoptions": {"size": "20", "maxlength": "40"},

			'width': 100
		},

		'buyer_name': {
			"label": u"购买者",
			"sortable": False,

			"search": False,
			"sorttype": 'string',
			"searchoptions": {"sopt": ['le', 'lt', 'gt', 'ge']},

			'editable': False,
			# "edittype": 'text',
			# "editoptions": {"size": "20", "maxlength": "40"},

			'width': 100
		},

		'scan_province': {
			"label": u"扫码省份",
			"sortable": False,

			"search": False,
			"sorttype": 'string',
			"searchoptions": {"sopt": ['le', 'lt', 'gt', 'ge']},

			'editable': False,
			# "edittype": 'text',
			# "editoptions": {"size": "20", "maxlength": "40"},

			'width': 100
		},

		'scan_city': {
			"label": u"扫码城市",
			"sortable": False,

			"search": False,
			"sorttype": 'string',
			"searchoptions": {"sopt": ['le', 'lt', 'gt', 'ge']},

			'editable': False,
			# "edittype": 'text',
			# "editoptions": {"size": "20", "maxlength": "40"},

			'width': 100
		},

		'publish_province': {
			"label": u"发布省份",
			"sortable": False,

			"search": False,
			"sorttype": 'string',
			"searchoptions": {"sopt": ['le', 'lt', 'gt', 'ge']},

			'editable': False,
			# "edittype": 'text',
			# "editoptions": {"size": "20", "maxlength": "40"},

			'width': 100
		},

		'publish_price': {
			"label": u"建议售价",
			"sortable": False,

			"search": False,
			"sorttype": 'string',
			"searchoptions": {"sopt": ['le', 'lt', 'gt', 'ge']},

			'editable': False,
			# "edittype": 'text',
			# "editoptions": {"size": "20", "maxlength": "40"},

			'width': 100
		},
		'myac': {
			"label": u"操作",
			'editable': False,
			"width": 80,
			"fixed": True,
			"sortable": False,
			"search": False,
			"resize": False,
			"formatter": 'actions',
			"formatoptions": {"keys": True, "delbutton": True, }
		}
	}


@login_required(login_url="/smAdmin/login")
@permission_required(['submenu.statistics.scan_log', ])
def scan_log(request):
	"""
	扫码记录
	:param request:
	:return:
	"""
	grid = WxmpScanLogGrid()
	context = {
		"grid_config": grid.get_config(),
	}
	return render(request, "smAdmin/statistics/scan_log.html", context=context)


@login_required(login_url="/smAdmin/login")
@permission_required(['statistics.scan_log.list', ])
def scan_log_list(request):
	grid = WxmpScanLogGrid()
	return HttpResponse(grid.get_json(request), content_type="application/json")


@login_required(login_url="/smAdmin/login")
@permission_required(['statistics.scan_log.detail', ])
def scan_log_detail(request):
	"""
	扫描记录详情
	:param request:
	:return:
	"""
	id = request.GET.get('id')

	smerperpinfo = SmerpErpinfo.objects.get(id=id)
	smerpaccount = smerperpinfo.smerpaccount

	# 企业信息
	erp_info = []
	for field in smerperpinfo._meta.fields:
		field_name = field.name

		field_value = getattr(smerperpinfo, field.name)
		if field_value is None:
			field_value = ""
		if field_name in ("id", "smerpaccount"):
			continue

		# 标签
		label = field.verbose_name
		if field.choices:
			for item in field.choices:
				if field_value == item[0]:
					field_value = item[1]
					break

		erp_info.append({"name": field_name, "label": label, "value": field_value})

	# 企业账号信息
	account = []
	for field in smerpaccount._meta.fields:
		field_name = field.name
		label = field.verbose_name
		field_value = getattr(smerpaccount, field.name)
		if field_value is None:
			field_value = ""

		# 标签
		label = field.verbose_name
		if field.choices:
			print field.choices
			for item in field.choices:
				print item
				if field_value == item[0]:
					field_value = item[1]
					break

		account.append({"name": field_name, "label": label, "value": field_value})

	context = {
		"erp_info": erp_info,
		"account": account,
	}
	return render(request, "smAdmin/statistics/scan_log_detail.html", context=context)


@login_required(login_url="/smAdmin/login")
@permission_required(['submenu.statistics.realtime', ])
def realtime(request):
	"""
	实时统计
	:param request:
	:return:
	"""
	# grid = WxmpScanLogGrid()
	context = {
		# "grid_config": grid.get_config(),
	}
	return render(request, "smAdmin/statistics/realtime.html", context=context)


@login_required(login_url="/smAdmin/login")
@permission_required(['statistics.realtime.chart', ])
def realtime_chart(request):
	data = {}
	return HttpResponse(json.dumps(data), content_type="application/json")


@login_required(login_url="/smAdmin/login")
@permission_required(['submenu.statistics.region', ])
def region(request):
	"""
	地域分布
	:param request:
	:return:
	"""
	# grid = WxmpScanLogGrid()
	context = {
		# "grid_config": grid.get_config(),
	}
	return render(request, "smAdmin/statistics/region.html", context=context)


@login_required(login_url="/smAdmin/login")
@permission_required(['statistics.region.chart', ])
def region_chart(request):
	data = {}
	return HttpResponse(json.dumps(data), content_type="application/json")
